package com.asp.alumnisystem.presenter;

import com.asp.alumnisystem.base.BaseBean;
import com.asp.alumnisystem.base.BasePresenter;
import com.asp.alumnisystem.base.IView;
import com.asp.alumnisystem.model.UserModel;
import com.asp.alumnisystem.net.RetrofitManager;
import com.asp.alumnisystem.utils.StringUtils;
import com.tencent.mmkv.MMKV;

import io.reactivex.Observable;

/**
 * 登录使用到的Presenter
 */
public class LoginPresenter extends BasePresenter<Boolean, UserModel> {

    private String loginUser;
    private String loginPass;
    private String loginIp;

    public LoginPresenter(IView view) {
        super(view);
    }

    public void setLoginInfo(String loginUser, String loginPass, String loginIp){
        this.loginUser = loginUser;
        this.loginPass = loginPass;
        this.loginIp = loginIp;
    }

    @Override
    protected boolean filter(Boolean isVisitor) {

        //游客登录不检测
        if (!isVisitor){
            if (StringUtils.isNull(loginUser)){
                throw new RuntimeException("登录名不能为空");
            }

            if (StringUtils.isNull(loginPass)){
                throw new RuntimeException("登录密码不能为空");
            }
            MMKV.defaultMMKV().encode("loginUser", loginUser);
        }else{
            UserModel userModel = new UserModel();
            userModel.setUserId(-1L);
            userModel.setNickName("游客");
            userModel.setIconPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543380981993&di=d4aa488b0feb31c4952985984ae0ff13&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F2e2eb9389b504fc252937ea7e6dde71191ef6d40.jpg");
            userModel.setCompany("游客豪华班");
            userModel.setVisitor(true);
            success(BaseBean.requestSuccess(userModel));
            return false;
        }
        return true;
    }

    @Override
    protected Observable<BaseBean<UserModel>> requestNet(Boolean aBoolean) {
        return RetrofitManager.getApi(loginIp).login(loginUser, loginPass);
    }


}
